package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.coupon.client.CouponQueryClient;
import com.woniuxy.coupon.client.CouponTypeManageVClient;
import com.woniuxy.coupon.client.CouponTypeQueryClient;
import com.woniuxy.coupon.client.param.AddCouponTypeParam;
import com.woniuxy.coupon.client.param.ModifyCouponTypeParam;
import com.woniuxy.coupon.client.param.QueryCouponTypeParam;
import com.woniuxy.coupon.client.result.CouponTypeDto;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.CouponTypeClient;
import com.woniuxy.store.client.param.couponParam.ModifyCouponTypeStateParams;
import com.woniuxy.store.client.param.couponTypeParam.AddCouponTypeParams;
import com.woniuxy.store.client.param.couponTypeParam.ModifyCouponTypeParams;
import com.woniuxy.store.client.param.couponTypeParam.QueryCouponTypeParams;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.CommonResultCodeEnum;
import result.Result;

import javax.validation.Valid;

@RestController
@RequestMapping("/store/marketing")
public class CouponTypeClientImpl implements CouponTypeClient {
    @Autowired
    private CouponTypeManageVClient couponTypeManageVClient;
    @Autowired
    private CouponTypeQueryClient couponTypeQueryClient;
    @Override
    @Log("添加优惠卷类型")
    public Result addCouponType(AddCouponTypeParams param) {
        Integer storeId = StoreIdUtil.getFromToken();
        AddCouponTypeParam addCouponTypeParam = BeanUtil.toBean(param, AddCouponTypeParam.class);
        addCouponTypeParam.setStoreId(storeId);
        return couponTypeManageVClient.addCouponType(addCouponTypeParam);
    }

    @Override
    @Log("删除优惠卷类型")
    public Result deleteCouponType(Integer couponTypeId) {
        Integer storeId = StoreIdUtil.getFromToken();
        Result<CouponTypeDto> resultType = couponTypeQueryClient.queryCouponTypeByTypeId(couponTypeId);
        if (resultType.getData()==null) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (resultType.getData().getStoreId()!=storeId) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        return couponTypeManageVClient.deleteCouponType(storeId,couponTypeId);
    }

    @Override
    @Log("修改优惠卷类型信息")
    public Result modifyCouponType(@Valid ModifyCouponTypeParams param) {
        Integer storeId = StoreIdUtil.getFromToken();
        Result<CouponTypeDto> resultType = couponTypeQueryClient.queryCouponTypeByTypeId(param.getCouponTypeId());
        if (resultType.getData()==null) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (resultType.getData().getStoreId()!=storeId) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        ModifyCouponTypeParam modifyCouponTypeParam = BeanUtil.toBean(param, ModifyCouponTypeParam.class);
        modifyCouponTypeParam.setStoreId(storeId);
        return couponTypeManageVClient.modifyCouponType(modifyCouponTypeParam);
    }

    @Override
    @Log("开启关闭优惠卷叠加")
    public Result modifyCouponTypeSuperposition(Integer superposition, Integer couponTypeId) {
        Integer storeId = StoreIdUtil.getFromToken();
        Result<CouponTypeDto> resultType = couponTypeQueryClient.queryCouponTypeByTypeId(couponTypeId);
        if (resultType.getData()==null) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (resultType.getData().getStoreId()!=storeId) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        return couponTypeManageVClient.modifyCouponTypeSuperposition(superposition,couponTypeId);
    }

    @Override
    @Log("开启关闭优惠卷使用")
    public Result modifyCouponTypeState(@Valid ModifyCouponTypeStateParams modifyCouponTypeStateParams) {
        Integer storeId = StoreIdUtil.getFromToken();
        Result<CouponTypeDto> resultType = couponTypeQueryClient.queryCouponTypeByTypeId(modifyCouponTypeStateParams.getCouponTypeId());
        if (resultType.getData()==null) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (resultType.getData().getStoreId()!=storeId) {
            throw new MallException("当前优惠卷类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        return couponTypeManageVClient.modifyCouponTypeState(modifyCouponTypeStateParams.getCouponTypeState(),modifyCouponTypeStateParams.getCouponTypeId());
    }

    @Override
    @Log("根据条件查询优惠卷类型")
    public Result queryCouponType(@Valid QueryCouponTypeParams param) {
        Integer storeId = StoreIdUtil.getFromToken();
        QueryCouponTypeParam queryCouponTypeParam = BeanUtil.toBean(param, QueryCouponTypeParam.class);
        queryCouponTypeParam.setStoreId(storeId);
        return couponTypeQueryClient.queryCouponType(queryCouponTypeParam);
    }
}
